<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="餐桌号" prop="tableNum">
        <el-input
          v-model="queryParams.tableNum"
          placeholder="请输入餐桌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总价" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入总价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就餐人数" prop="dinnerNum">
        <el-input
          v-model="queryParams.dinnerNum"
          placeholder="请输入就餐人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="currentStatus">
        <el-select v-model="queryParams.currentStatus" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="item in orderOptions"
            :key="item.dictCode"
            :label="item.dictLabel"
            :value="item.dictLabel">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payMethod">
        <el-input
          v-model="queryParams.payMethod"
          placeholder="请输入支付方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['scancode:orderMaster:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderMasterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="餐桌号" align="center" prop="tableNum" />
      <el-table-column label="订单编号" align="center" prop="id" />
      <el-table-column label="就餐人数" align="center" prop="dinnerNum" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="订单状态" align="center" prop="currentStatus" />
      <el-table-column label="支付方式" align="center" prop="payMethod" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewDetail(scope.row)"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog title="订单详情" :visible.sync="dialogVisibleOrder" width="30%" :before-close="handleClose">
        <el-form label-position="right" label-width="80px">
        <el-form-item label="订单编号">{{ orderDetailData.id }}</el-form-item>
        <el-form-item label="餐桌号">{{ orderDetailData.tableNum }}</el-form-item>
        <el-form-item label="就餐人数">{{ orderDetailData.dinnerNum}}人</el-form-item>
        <el-form-item label="订单状态">{{ orderDetailData.currentStatus }}</el-form-item>
        <el-form-item label="已点菜品">( 共{{ totalFood }}份 共 {{  orderDetailData.totalPrice}})</el-form-item>
            <div v-for="(item, index) in Foodimg" :key="item.id"  style="display: flex;margin: 5px 30px;align-items: center;">
              <img :src="item.image" alt="" width="50px" height="50px"/>
              <div style="flex: 1;margin-left: 10px;">
                <div style="font-weight:bold">{{item.tasteName}}</div>
                <div>{{item.size}}</div>
                <div>x{{ foodNum[index].number }}份</div>
              </div>
              <span>￥{{item.price}}</span>
          </div>
        <el-form-item label="用餐时间">{{ formatDate(orderDetailData.createTime) }}</el-form-item>
        <el-form-item label="支付方式">{{ orderDetailData.payMethod }}</el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleOrder = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisibleOrder = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderMaster, getOrderMaster, orderDetail, orderDetailNum, orderDetailimg, exportOrderMaster } from "@/api/scancode/orderMaster";
import { formatDate } from '@/utils'

export default {
  name: "OrderMaster",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点餐表格数据
      orderMasterList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderStateId: null,
        businessId: null,
        totalPrice: null,
        dinnerNum: null,
        remarks: null,
        tableNum: null,
        currentStatus: null,
        payMethod: null,
      },
      orderOptions:[],
      dialogVisibleOrder: false,
      orderDetailData: {},
      Foodimg: [],
      totalFood: '',
      foodNum: []
    };
  },
  created() {
    this.getList();
    this.getDicts("order_status").then(response => {
      this.orderOptions = response.data;
    });
  },
  methods: {
    formatDate,
    /** 查询点餐列表 */
    getList() {
      this.loading = true;
      listOrderMaster(this.queryParams).then(response => {
        this.orderMasterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderStateId: null,
        businessId: null,
        totalPrice: null,
        dinnerNum: null,
        remarks: null,
        tableNum: null,
        currentStatus: "0",
        payMethod: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有点餐数据项？').then(() => {
        this.exportLoading = true;
        return exportOrderMaster(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    // 查看对话框
    viewDetail(row){
      this.dialogVisibleOrder = true
      orderDetail(row.id).then( res => {
        this.orderDetailData = res.data
      })
      orderDetailimg(row.id).then( res => {
        this.Foodimg = res.rows
        this.totalFood = res.total
      })
      orderDetailNum(row.id).then( res => {
        this.foodNum = res.rows
      })
    },
    // 取消对话框
    handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      }
  }
};
</script>
<style scoped>
/deep/ .el-form-item{
  margin-bottom: 5px;
}
</style>
